package com.liangxy.news.ui.splash;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.liangxy.news.R;
import com.liangxy.news.model.AdBanner;

import java.util.List;

/**
 * 广告轮播适配器
 */
public class AdBannerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_IMAGE = 1;
    private static final int TYPE_LAYOUT = 2;

    private final List<AdBanner> adBanners;
    private OnBannerClickListener clickListener;

    public AdBannerAdapter(List<AdBanner> adBanners) {
        this.adBanners = adBanners;
    }

    @Override
    public int getItemViewType(int position) {
        AdBanner banner = adBanners.get(position);
        if (banner.isCustomLayout()) {
            return TYPE_LAYOUT;
        }
        return TYPE_IMAGE;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_LAYOUT) {
            // 使用自定义布局容器
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_ad_container, parent, false);
            return new LayoutViewHolder(view);
        } else {
            // 使用标准图片布局
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_ad_banner, parent, false);
            return new ImageViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        AdBanner banner = adBanners.get(position);
        
        if (holder instanceof LayoutViewHolder) {
            LayoutViewHolder layoutHolder = (LayoutViewHolder) holder;
            // 清除原有内容
            layoutHolder.container.removeAllViews();
            
            // 加载自定义布局
            View customView = LayoutInflater.from(holder.itemView.getContext())
                    .inflate(banner.getLayoutResId(), layoutHolder.container, false);
            
            // 添加到容器
            layoutHolder.container.addView(customView);
            
        } else if (holder instanceof ImageViewHolder) {
            ImageViewHolder imageHolder = (ImageViewHolder) holder;
            
            // 设置标题
            if (banner.getTitle() != null && !banner.getTitle().isEmpty()) {
                imageHolder.titleView.setVisibility(View.VISIBLE);
                imageHolder.titleView.setText(banner.getTitle());
            } else {
                imageHolder.titleView.setVisibility(View.GONE);
            }
            
            // 加载图片
            if (banner.isLocalImage()) {
                // 本地图片资源
                Glide.with(imageHolder.imageView.getContext())
                    .load(banner.getLocalImageResId())
                    .placeholder(R.drawable.placeholder_image)
                    .error(R.drawable.placeholder_image)
                    .centerCrop()
                    .into(imageHolder.imageView);
            } else if (banner.getImageUrl() != null) {
                // 网络图片
                Glide.with(imageHolder.imageView.getContext())
                    .load(banner.getImageUrl())
                    .placeholder(R.drawable.placeholder_image)
                    .error(R.drawable.placeholder_image)
                    .centerCrop()
                    .into(imageHolder.imageView);
            }
        }
        
        // 为所有类型的ViewHolder设置点击事件
        holder.itemView.setOnClickListener(v -> {
            if (clickListener != null) {
                clickListener.onBannerClick(banner, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return adBanners.size();
    }
    
    @Override
    public void onViewAttachedToWindow(@NonNull RecyclerView.ViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
        if (lp instanceof ViewGroup.MarginLayoutParams) {
            ((ViewGroup.MarginLayoutParams) lp).width = ViewGroup.MarginLayoutParams.MATCH_PARENT;
            ((ViewGroup.MarginLayoutParams) lp).height = ViewGroup.MarginLayoutParams.MATCH_PARENT;
            holder.itemView.setLayoutParams(lp);
        }
    }

    static class ImageViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView titleView;

        ImageViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.ad_image);
            titleView = itemView.findViewById(R.id.ad_title);
        }
    }
    
    static class LayoutViewHolder extends RecyclerView.ViewHolder {
        FrameLayout container;
        
        LayoutViewHolder(@NonNull View itemView) {
            super(itemView);
            container = itemView.findViewById(R.id.ad_container);
        }
    }

    public void setOnBannerClickListener(OnBannerClickListener listener) {
        this.clickListener = listener;
    }

    public interface OnBannerClickListener {
        void onBannerClick(AdBanner banner, int position);
    }
} 